package com.linghu.qqclient.service;

import com.linghu.qqcommon.Message;
import com.linghu.qqcommon.MessageType;
import com.linghu.qqservice.service.ManagerClientThreads;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * @author 令狐荣豪
 * @version 1.0
 * 该类对象完成 文件传输
 */
public class FileClientService {
    /**
     *
     * @param src 源文件
     * @param dest 文件传输到哪里
     * @param senderId 发送用户id
     * @param getterId 接收文件用户id
     */
    public void senFileToOne(String src,String dest,String senderId,String getterId) throws IOException {
        //读取src文件 封装到message对象里面
        Message message=new Message();
        message.setMesType(MessageType.MESSAGE_FILE_MES);
        message.setSender(senderId);
        message.setGetter(getterId);

        message.setSrc(src);
        message.setDest(dest);

        //需要将文件读取
        FileInputStream fileInputStream=null;
        byte fileBytes[] = new byte[1024];

        try {
            fileInputStream=new FileInputStream(src);
            fileInputStream.read(fileBytes);//将src文件读入到程序的字节数组中
            //将文件对应的字节数组设置到message
            message.setFileBytes(fileBytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            fileInputStream.close();
        }
        //提示信息
        System.out.println("\n"+message.getSender()+"给"+getterId+"发送文件："+src
        +"到对方电脑的目录："+dest);

        //发送
        ObjectOutputStream oos = new
                ObjectOutputStream(ManagerClientThreads.geterverConnectClientThread(senderId).getSocket().getOutputStream());
        oos.writeObject(message);
    }
}
